﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using MuaChung.Base;
using MuaChung.doanhnghiepbiz;
using MuaChung.beatifulhousews;

namespace MuaChung.Models
{
    public class RegisterCouponModel
    {
        public string maCoupon { get; set; }
        public bool status { get; set; }
    }
    public class WSLogonModel
    {
        public string serviceID { get; set; }
        [Required]
        [DisplayName("User name")]
        public string UserName { get; set; }

        [Required]
        [DataType(DataType.Password)]
        [DisplayName("Password")]
        public string Password { get; set; }
    }

    public class Service_Methods
    {
        public string serviceName { get; set; }
        public List<string> serviceMethods { get; set; }
    }
    public class WSDynamicModel
    {
        public string serviceURL { get; set; }
        public List<Service_Methods> services { get; set; }
        public DOANHNGHIEP doanhNghiep { get; set; }
    }
    public class SelectTransportModel
    {
        public string CustomerName { get; set; }
        public ContactInfo info { get; set; }
        public List<TransportCompany> lstcmp { get; set; }
    }
    public class ServiceModel
    {
        private static MuaChungEntities db = new MuaChungEntities();
        public static WSDynamicModel currentWS = new WSDynamicModel();

        public static List<Coupon> lstTmpCoupons = new List<Coupon>();

        public static Coupon FindTmpCoupon(string maCoupon)
        {
            foreach (var item in lstTmpCoupons)
            {
                if (item.MaCoupon == maCoupon)
                {
                    return item;
                }
            }
            return null;
        }
        public static List<WEBSERVICE> GetAllWS()
        {
            try
            {
                var query = from n in db.WEBSERVICEs                            
                            select n;
                return query.ToList();
            }
            catch { return null; }
        }
        public static List<WEBSERVICE> GetAllWSByType(string type)
        {
            try
            {
                var query = from n in db.WEBSERVICEs
                            where n.LoaiWS == type
                            select n;
                return query.ToList();
            }
            catch { return null; }
        }
        public static bool AddWS(WEBSERVICE ws)
        {
            try
            {
                db.AddToWEBSERVICEs(ws);
                db.SaveChanges();
                return true;
            }
            catch { return false; }
        }        
        public static WEBSERVICE FindWSByID(int id)
        {
            try
            {
                var query = from n in db.WEBSERVICEs
                            where n.ID == id
                            select n;
                return query.First();
            }
            catch { return null; }
        }
        public static WEBSERVICE FindWSByURL(string url)
        {
            try
            {
                var query = from n in db.WEBSERVICEs
                            where n.URL == url
                            select n;
                return query.First();
            }
            catch { return null; }
        }
        public static List<Coupon> GetListCoupon(WEBSERVICE sv)
        {
            WebServiceDoanhNghiepBiz ws = new WebServiceDoanhNghiepBiz();
            Service1 ws1 = new Service1();
            try
            {
                if (sv.Ma_WS == BaseWeb.WS_BIZ5A)
                {
                    string sid = ws.Authenticate(sv.WSUSERNAME, sv.WSPASSWORD);
                    Coupon[] lstcoupon = ws.FindAllCoupons(sid);
                    return lstcoupon.ToList();
                }
                else if (sv.Ma_WS == BaseWeb.WS_BIZ2B)
                {
                    string sid = ws1.Authenticate(sv.WSUSERNAME, sv.WSPASSWORD);
                    Coupon[] lstcoupon = ws1.FindAllCoupons(sid);
                    return lstcoupon.ToList();
                }
                else
                {
                    return null;
                }
            }
            catch
            {
                return null;
            }
        }
        public static List<RegisterCouponModel> RegisterCoupon(WEBSERVICE sv, List<string> lstMaCoupon)
        {
            WebServiceDoanhNghiepBiz ws = new WebServiceDoanhNghiepBiz();
            Service1 ws1 = new Service1();
            List<RegisterCouponModel> lstcode = new List<RegisterCouponModel>();
            string sid = null;
            try
            {
                if (sv.Ma_WS == BaseWeb.WS_BIZ5A)
                {
                    sid = ws.Authenticate(sv.WSUSERNAME, sv.WSPASSWORD);
                    foreach (var item in lstMaCoupon)
                    {
                        RegisterCouponModel code = new RegisterCouponModel();
                        code.status = ws.RegisterCoupon(sid, item);
                        code.maCoupon = item;
                        lstcode.Add(code);
                    }
                }
                else if (sv.Ma_WS == BaseWeb.WS_BIZ2B)                
                {
                    sid = ws1.Authenticate(sv.WSUSERNAME, sv.WSPASSWORD);
                    foreach (var item in lstMaCoupon)
                    {
                        RegisterCouponModel code = new RegisterCouponModel();
                        code.status = ws1.RegisterCoupon(sid, item);
                        code.maCoupon = item;
                        lstcode.Add(code);
                    }
                }               
                return lstcode;
            }
            catch { return null; }
        }
    }
}